package com.app.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.app.application.MyApplication;
import com.core.lib.utils.main.LogUtilBase;
import com.lepu.app.main.permission.PermissionUtil;
import com.lepu.app.main.permission.callback.PermissionResultCallBack;

import java.util.List;

/**
 * Created by wxd on 2016/11/1.
 */
public class LocationUtil implements LocationListener {

    private static LocationUtil mInstance = null;

    public float mAccuracy = 0f; // 精确度
    public double mAltitude = 0d; // 海拔
    public double mLatitude = 0d; // 纬度
    public double mLongitude = 0d; // 经度

    private LocationUtil() {

    }

    public static LocationUtil getInstance() {
        if (mInstance == null) {
            mInstance = new LocationUtil();
        }
        return mInstance;
    }

    public void getLocation(final Activity context) {
        PermissionUtil.getInstance().request(context, new String[]{
                        Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.ACCESS_COARSE_LOCATION
                },
                new PermissionResultCallBack() {
                    @Override
                    public void onPermissionGranted() {

                        //获取定位管理对象
                        try {
                            LocationManager lm = (LocationManager) MyApplication.getInstance().getSystemService(Context.LOCATION_SERVICE);
                            List<String> names = lm.getAllProviders();//获取所有的位置提供者，一般三种

                            Criteria criteria = new Criteria();//查询条件，如果设置了海拔，则定位方式只能是GPS;
                            criteria.setCostAllowed(true);//是否产生开销，比如流量费
                            String provider = lm.getBestProvider(criteria, true);//获取最好的位置提供者，第二个参数为true，表示只获取那些被打开的位置提供者
                            lm.requestLocationUpdates(provider, 0l, 0f, LocationUtil.this);
                            //获取位置。第二个参数表示每隔多少时间返回一次数据，第三个参数表示被定位的物体移动每次多少米返回一次数据。

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onPermissionGranted(String... permissions) {
                        if (permissions == null || permissions.length == 0) {
                        }
                    }

                    @Override
                    public void onPermissionDenied(String... permissions) {
                        if (permissions != null) {

                        }
                    }

                    @Override
                    public void onRationalShow(String... permissions) {
                        if (permissions != null) {
                        }
                    }
                });
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            mAccuracy = location.getAccuracy(); // 精确度 多少米
            mAltitude = location.getAltitude(); // 海拔
            mLatitude = location.getLatitude(); // 纬度
            mLongitude = location.getLongitude(); // 经度
        }
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        LogUtilBase.LogD(null, "LocationUtil onStatusChanged");
    }

    @Override
    public void onProviderEnabled(String provider) {
        LogUtilBase.LogD(null, "LocationUtil onProviderEnabled");
    }

    @Override
    public void onProviderDisabled(String provider) {
        LogUtilBase.LogD(null, "LocationUtil onProviderDisabled");
    }
}
